package org.jeecg.modules.villageCadreAssist.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.villageCadreAssist.entity.VcaEducation;
import org.jeecg.modules.villageCadreAssist.service.IVcaEducationService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: vca_education
 * @Author: jeecg-boot
 * @Date:   2022-02-28
 * @Version: V1.0
 */
@Api(tags="vca_education")
@RestController
@RequestMapping("/villageCadreAssist/vcaEducation")
@Slf4j
public class VcaEducationController extends JeecgController<VcaEducation, IVcaEducationService> {
	@Autowired
	private IVcaEducationService vcaEducationService;

	/**
	 * 分页列表查询
	 *
	 * @param vcaEducation
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "vca_education-分页列表查询")
	@ApiOperation(value="vca_education-分页列表查询", notes="vca_education-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(VcaEducation vcaEducation,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<VcaEducation> pageList = list(vcaEducation,pageNo,pageSize,req);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param vcaEducation
	 * @return
	 */
	@AutoLog(value = "vca_education-添加")
	@ApiOperation(value="vca_education-添加", notes="vca_education-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VcaEducation vcaEducation) {
		vcaEducationService.save(vcaEducation);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param vcaEducation
	 * @return
	 */
	@AutoLog(value = "vca_education-编辑")
	@ApiOperation(value="vca_education-编辑", notes="vca_education-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody VcaEducation vcaEducation) {
		vcaEducationService.updateById(vcaEducation);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "vca_education-通过id删除")
	@ApiOperation(value="vca_education-通过id删除", notes="vca_education-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		vcaEducationService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "vca_education-批量删除")
	@ApiOperation(value="vca_education-批量删除", notes="vca_education-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.vcaEducationService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "vca_education-通过id查询")
	@ApiOperation(value="vca_education-通过id查询", notes="vca_education-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VcaEducation vcaEducation = vcaEducationService.getById(id);
		if(vcaEducation==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(vcaEducation);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param vcaEducation
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, VcaEducation vcaEducation) {
		IPage<VcaEducation> page = list(vcaEducation, 0, Integer.MAX_VALUE, request);
		return super.exportXls(request, VcaEducation.class, "学历",page.getRecords());
    }

	 private IPage<VcaEducation> list(VcaEducation vcaEducation,Integer pageNo,Integer pageSize,HttpServletRequest req){
		 QueryWrapper<VcaEducation> queryWrapper = QueryGenerator.initQueryWrapper(vcaEducation, req.getParameterMap());
		 Page<VcaEducation> page = new Page<VcaEducation>(pageNo, pageSize);
		 LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 String orgCode = user.getOrgCode();
		 String paramCode = vcaEducation.getString("orgCode");
		 if(paramCode!=null && paramCode.startsWith(orgCode)){//只能查看区域内的
			 orgCode = paramCode;
		 }
		 return vcaEducationService.queryPageList(page, queryWrapper,orgCode);
	 }
    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		List<VcaEducation> list = super.importExcel(request, VcaEducation.class);

		vcaEducationService.saveExcelBatch(list);


		return Result.ok("文件导入成功！数据行数：" + list.size());
    }

}
